<template>
  <div v-loading="loading">
    <el-form :inline="true" :model="roleFm" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="roleFm.roleName" placeholder="角色名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="handleGetRoleList">搜索</el-button>
        <el-button type="primary" @click="handleOpenRoleInsert">新增</el-button>
        <el-button type="warning" @click="handleOpenRoleEdit">编辑</el-button>
        <el-button type="danger" @click="handleOpenRoleDelete">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      ref="singleTable"
      highlight-current-row
      @current-change="handleTableChange"
      style="width: 100%">
      <el-table-column
        prop="roleId"
        label="角色编号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="roleName"
        label="角色名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="roleFm.pageNum"
      :page-sizes="[10, 20 ,30]"
      :page-size="roleFm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="roleFm.total">
    </el-pagination>


    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :before-close="handleClose">
      <el-form ref="form" :model="saveRole" label-width="100px">
        <el-form-item label="角色名称：">
          <el-input v-model="saveRole.roleName"></el-input>
        </el-form-item>
        <el-form-item label="所属部门：">
          <el-input v-model="saveRole.deptName" readonly @focus="deptDialogVisible=true"></el-input>
          <el-input v-model="saveRole.deptId" v-show="false"></el-input>
          <el-dialog
            append-to-body
            :visible.sync="deptDialogVisible"
            width="30%"
            :before-close="handleClose">
            <el-tree node-key="deptId" accordion :data="depts" ref="dept" :props="DeptdefaultProps" @node-click="handleDeptNodeClick"></el-tree>
              <span slot="footer" class="dialog-footer">
            <el-button @click="deptDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="deptDialogVisible = false">确 定</el-button>
          </span>
          </el-dialog>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="saveRole.remark">
          </el-input>
        </el-form-item>
        <el-form-item label="功能权限：">
          <el-tree v-model="saveRole.menuIds" node-key="menuId" :data="menus" ref="tree" show-checkbox :props="defaultProps" @node-click="handleNodeClick"></el-tree>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" v-if="handleShowInsertButton" @click="handleRoleInsert">新 增</el-button>
    <el-button type="primary" v-if="handleShowEditButton" @click="handleRoleEdit" v-loading="updLoad">编 辑</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "roleList",
  data(){
    return{
      updLoad:false,
      deptDialogVisible:false,
      loading:true,
      DeptdefaultProps:{
        value:"deptId",
        children: 'childMenus',
        label: 'name',
      },
      defaultProps: {
        value:"menuId",
        children: 'menuList',
        label: 'name',
      },
      title:"",
      dialogVisible:false,
      handleShowInsertButton:false,
      handleShowEditButton:false,
      tableRow:{},
      tableData:[],
      saveRole:{
        menuIds:[]
      },
      roleFm:{
        pageNum:1,
        pageSize:10,
        total:0,
        roleName:'',
      },
      depts:[],
      menus:[],
    }
  },
  methods:{
    handleDeptNodeClick(data){
      this.saveRole.deptId=data.deptId
      this.saveRole.deptName=data.name
      this.deptDialogVisible=false;
    },
    handleRoleEdit(){
      this.updLoad=true
      this.saveRole.menuIds= this.$refs.tree.getCheckedKeys();
      axios.post("http://localhost:8200/sys/role/edit",this.saveRole).then(res=>{
        if (res.data.code==200){
          this.$message({
            message: res.data.msg,
            type: 'success'
          });
          this.dialogVisible=false;
          this.handleGetRoleList()
        }else{
          this.$message({
            message: res.data.msg,
            type: 'error'
          });
        }
        this.updLoad=false;
      })
    },
    handleRoleInsert(){

      //获取复选框key
      let aaa=[];
      this.saveRole.menuIds= this.$refs.tree.getCheckedKeys();
      axios.post("http://localhost:8200/sys/role/insert",this.saveRole).then(res=>{
        if (res.data.code==200){
          this.$message({
            message: res.data.msg,
            type: 'success'
          });
          this.dialogVisible=false;
          this.handleGetRoleList()
        }else{
          this.$message({
            message: res.data.msg,
            type: 'error'
          });
        }
      })
    },
    handleOpenDeptTree(){
      alert("dadad")
      this.deptDialogVisible=true;
    },
    handleOpenRoleDelete(){
      if (this.tableRow.roleId==null){
        alert("请选择");
        return ;
      }else{
        if (confirm("是否删除")){
          axios.post("http://localhost:8200/sys/role/delete",this.tableRow).then(res=>{
            if (res.data.code==200){
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
              this.handleGetRoleList()
            }else{
              this.$message({
                message: res.data.msg,
                type: 'error'
              });
            }
          })
        }
      }
    },
    handleOpenRoleEdit(){
      if (this.tableRow.roleId==null){
        alert("请选择");
        return ;
      }else{
        this.title="编辑"
        this.saveRole=this.tableRow;
        this.dialogVisible=true;
        this.handleShowInsertButton=false;
        this.handleShowEditButton=true;
        this.saveRole.deptName=this.saveRole.roleDept.name
        this.saveRole.deptId=this.tableRow.deptId
        //回显树
        this.$nextTick(()=>{
          this.$refs.tree.setCheckedKeys(this.saveRole.menuIds);
        })
      }
    },
    handleOpenRoleInsert(){
      if (this.tableRow.roleId!=null){
        alert("请勿选择");
        this.setCurrent(null);
        this.tableRow={}
        return ;
      }else{
        this.title="新增"
        this.dialogVisible=true;
        this.handleShowInsertButton=true;
        this.handleShowEditButton=false;
      }
    },
    handleNodeClick(data) {

        console.log(data);
    },
    setCurrent(row) {
      this.$refs.singleTable.setCurrentRow(row);
    },
    handleTableChange(val) {
      this.tableRow = val;
    },
    handleSizeChange(val) {
      this.roleFm.pageSize=val
      this.handleGetRoleList();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.roleFm.pageNum=val
      this.handleGetRoleList();
      console.log(`当前页: ${val}`);
    },

    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
          this.saveRole={}
          this.$nextTick(()=>{
            this.$refs.tree.setCheckedKeys([]);
          })
        })
        .catch(_ => {});
    },
    handleGetRoleList(){
      axios.post("http://localhost:8200/sys/role/list",this.roleFm).then(res=>{
        console.log(res)
        this.tableData=res.data.obj.list
        this.roleFm.pageNum=res.data.obj.pageNum
        this.roleFm.pageSize=res.data.obj.pageSize;
        this.roleFm.total=res.data.obj.total
        this.loading=false;
      })
    },
    handleGetDeptList(){
      axios.post("http://localhost:8200/sys/dept/list",{pageSize:100000}).then(res=>{
        this.depts=res.data.list
        console.log("部门："+this.depts)
      })
    },
    handleGetMenuList(){
      axios.post("http://localhost:8200/sys/menu/list").then(res=>{
        this.menus=res.data.obj
        console.log("菜单："+this.menus)
      })
    }
  },
  created() {
    this.handleGetRoleList();
    this.handleGetDeptList();
    this.handleGetMenuList();
  },
}
</script>

<style scoped>

</style>
